﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

namespace tests.cs.textid_formatters {

    using Str = System.Text.RegularExpressions;

    [TestClass]
    public class DotNetStringAlgorithms {
        Dictionary<String, String> m_Regexes;

        String WriteResult(String p_In, IEnumerable<String> p_Exp, IEnumerable<String> p_Out)
        {
            var tOut = new System.IO.StringWriter();
            tOut.WriteLine("Input: {0}", p_In); 
            
            int tID = 0;
            tOut.WriteLine("Expectation:");
            foreach (var tToken in p_Exp) {
                tOut.WriteLine("token[{0}]: {1}", tID++, tToken);
            }

            tID = 0;
            tOut.WriteLine("Result:");
            foreach (var tToken in p_Out) {
                tOut.WriteLine("token[{0}]: {1}", tID++, tToken);
            }

            return tOut.ToString();
        }

        [TestInitialize]
        public void SetUp()
        {
            m_Regexes = new Dictionary<string, string>() {
                { 
                    "literal_plural_regex", 
                    @"(""(?:[^\\""]|\\.)*"")|(Plural[\s]*\([\s]*\))"
                }
            };
        }

        [TestCleanup]
        public void TearDown()
        {
            m_Regexes.Clear();
        }

        [TestMethod]
        [TestProperty("Module", "DotNetStringAlgorithms")]
        public void match_literal_plural_regex()
        {
            var t_Input =
                "Localize(\"You have {1} message\", \"You have {1} messages\", Plural())";
            
            var t_Result = new List<String>();
            foreach (Str.Match t_Match in Str.Regex
                .Matches(t_Input, m_Regexes["literal_plural_regex"])) {

                    Assert.IsTrue(t_Match.Success);
                    t_Result.Add(t_Match.Value);
            }

            var t_Exp = new String[] {
                "\"You have {1} message\"",
                "\"You have {1} messages\"",
                "Plural()"
            };

            var tOut = this.WriteResult(t_Input, t_Exp, t_Result);
            Console.Write(tOut);
            CollectionAssert.AreEqual(t_Exp, t_Result, StringComparer.InvariantCulture, tOut);
        }

        [TestMethod]
        [TestProperty("Module", "DotNetStringAlgorithms")]
        public void split_literal_plural_regex()
        {
            var t_Input =
                "Localize(\"You have {1} message\", \"You have {1} messages\", Plural())";
            
            var t_Exp = new String[] {
                "Localize(",
                "\"You have {1} message\"",
                ",", 
                "\"You have {1} messages\"",
                ",",
                "Plural()",
                ")"
            };

            var t_Result = Str.Regex
                .Split(t_Input, m_Regexes["literal_plural_regex"])
                .Select(pToken => pToken.Trim()).ToArray();

            var tOut = this.WriteResult(t_Input, t_Exp, t_Result);
            Console.Write(tOut);
            CollectionAssert.AreEqual(t_Exp, t_Result, StringComparer.InvariantCulture, tOut);
        }
    }
}
